<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="学生姓名" prop="studentName">
        <el-input v-model="queryParams.studentName" placeholder="请输入学生姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="学生邮箱" prop="email">
        <el-input v-model="queryParams.email" placeholder="请输入学生邮箱" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="学生微信" prop="wechat">
        <el-input v-model="queryParams.wechat" placeholder="请输入学生微信" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="学生电话" prop="phone">
        <el-input v-model="queryParams.phone" placeholder="请输入学生电话" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="班级名" prop="className">
        <el-input v-model="queryParams.className" placeholder="请输入班级名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="班级id" prop="classId">
        <el-input v-model="queryParams.classId" placeholder="请输入班级id" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="创建时间" prop="creatTime">
        <el-date-picker clearable v-model="queryParams.creatTime" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="老师姓名" prop="teacherName">
        <el-input v-model="queryParams.teacherName" placeholder="请输入老师姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="老师id" prop="teacherId">
        <el-input v-model="queryParams.teacherId" placeholder="请输入老师id" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option v-for="dict in dict.type.status" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['employment:studentHelp:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['employment:studentHelp:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['employment:studentHelp:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['employment:studentHelp:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="studentHelpList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="老师姓名" align="center" prop="teacherName" />
      <el-table-column label="学生id" align="center" prop="id" />
      <el-table-column label="学生姓名" align="center" prop="studentName" />
      <el-table-column label="学生微信" align="center" prop="wechat" />
      <el-table-column label="班级名" align="center" prop="className" />
      <el-table-column label="班级id" align="center" prop="classId" />
      <el-table-column label="创建时间" align="center" prop="creatTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.creatTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="问题类型" align="center" prop="issueType" />

      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <div v-if="scope.row.status === 1" style="background-color: #e7faf0; border-radius: 10%;">成功处理</div>
          <div v-else-if="scope.row.status === 0" style="background-color: #e4f1ff; border-radius: 10%;">待处理</div>
          <div v-else-if="scope.row.status === 2" style="background-color: #e4f1ff; border-radius: 10%;">未处理</div>
          <div v-else style="background-color: #e4f1ff; border-radius: 10%;">已移交</div>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope" style="display: flex; justify-content: space-between;">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['employment:studentHelp:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['employment:studentHelp:remove']">删除</el-button>

          <!-- 新增按钮 -->
          <el-button v-if="scope.row.status === 0" size="mini" type="text" icon="el-icon-check"
            @click="handleComplete(scope.row)">完成</el-button>
          <el-button v-if="scope.row.status === 0" size="mini" type="text" icon="el-icon-message"
            @click="handleRemindTransfer(scope.row)">移交</el-button>
          <el-button v-if="scope.row.status === 0" size="mini" type="text" icon="el-icon-message"
            @click="handleRemindTransf(scope.row)">提醒</el-button>
        </template>
        <!-- 完成的弹窗 -->
        <el-dialog title="完成帮扶" :visible.sync="dialogVisible" width="500px" append-to-body>
          <el-form ref="form" :model="form" :rules="rules" label-width="80px">
            <el-form-item label="学生姓名" prop="studentName">
              <el-input v-model="finishform.studentName" placeholder="请输入学生姓名" />
            </el-form-item>
            <el-form-item label="状态" prop="status">
              <el-select v-model="finishform.status" placeholder="请选择状态">
                <el-option label="待处理" value="0"></el-option>
                <el-option label="已经处理" value="1"></el-option>
                <el-option label="未处理" value="2"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="备注" prop="remark">
              <el-input v-model="finishform.remark" placeholder="请输入备注" />
            </el-form-item>
            <el-form-item label="完成时间" prop="creatTime">
              <el-date-picker clearable v-model="finishform.creatTime" type="date" value-format="yyyy-MM-dd"
                placeholder="请完成时间">
              </el-date-picker>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitFormTwo">确 定</el-button>
            <el-button @click="cancelTwo">取 消</el-button>
          </div>
        </el-dialog>

        <!-- 帮扶移交的弹窗 -->
        <el-dialog :title="title" :visible.sync="moveHelpVisible" width="500px" append-to-body>
          <el-form ref="form" :model="helpMoveform" :rules="rules" label-width="80px">
            <el-form-item label="移交给谁" prop="accpectName">
              <el-select v-model="helpMoveform.accpectName" @change="teacherChange" placeholder="请选择老师"
                style="width: 300px;">
                <el-option v-for="item in teacherList" :key="item.id" :label="item.name" :value="item">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="移交备注" prop="moveRemark">
              <el-input v-model="helpMoveform.moveRemark" placeholder="请输入移交备注" />
            </el-form-item>
            <el-form-item label="发送邮箱" prop="sendEmail">
              <el-switch v-model="helpMoveform.sendEmail"></el-switch>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitFormThree">确 定</el-button>
            <el-button @click="cancelThree">取 消</el-button>
          </div>
        </el-dialog>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改就业帮扶对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="学生姓名" prop="studentName">
          <el-input v-model="form.studentName" placeholder="请输入学生姓名" />
        </el-form-item>
        <el-form-item label="学生邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入学生邮箱" />
        </el-form-item>
        <el-form-item label="学生微信" prop="wechat">
          <el-input v-model="form.wechat" placeholder="请输入学生微信" />
        </el-form-item>
        <el-form-item label="学生电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入学生电话" />
        </el-form-item>
        <el-form-item label="班级名" prop="className">
          <el-input v-model="form.className" placeholder="请输入班级名" />
        </el-form-item>
        <el-form-item label="班级id" prop="classId">
          <el-input v-model="form.classId" placeholder="请输入班级id" />
        </el-form-item>
        <el-form-item label="创建时间" prop="creatTime">
          <el-date-picker clearable v-model="form.creatTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="老师姓名" prop="teacherName">
          <el-input v-model="form.teacherName" placeholder="请输入老师姓名" />
        </el-form-item>
        <el-form-item label="老师id" prop="teacherId">
          <el-input v-model="form.teacherId" placeholder="请输入老师id" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio v-for="dict in dict.type.status" :key="dict.value" :label="parseInt(dict.value)">{{ dict.label
              }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { finishStudentHelp, listStudentHelp, getStudentHelp, delStudentHelp, addStudentHelp, updateStudentHelp } from "@/api/employment/studentHelp";
import { addHelpMove,sentEmail } from "@/api/employment/helpMove";
import { listTeacher } from "@/api/teacher/teacher";
export default {
  name: "StudentHelp",
  dicts: ['status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 就业帮扶表格数据
      studentHelpList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        studentName: null,
        email: null,
        wechat: null,
        phone: null,
        className: null,
        classId: null,
        creatTime: null,
        issueType: null,
        teacherName: null,
        teacherId: null,
        status: null,
      },

      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      teacherList: {
        id: null,
        name: null,
        email:null,
      },
      finishform: {
        id: null,
        studentName: null,
        teacherName: null,
        status: null,
        email:null,
        remark: null,
      },
      helpMoveform: {
        id: null,
        moveName: null,
        accpectName: null,
        helpId: null,
        creatTime: null,
        issueType: null,
        moveRemark: null,
        accpectNameId: null,
        moveNameId: null,
        email:null,
        sendEmail: false, // 初始状态（可自定义）
      },
      dialogVisible: false,
      moveHelpVisible: false,
    };
  },
  created() {
    this.getList();
  },
  methods: {

    //选择老师
    teacherChange(teacherValue) {
      this.helpMoveform.accpectName = teacherValue.name;
      this.helpMoveform.email = teacherValue.email;
      console.log(this.helpMoveform.accpectName);
      this.helpMoveform.accpectNameId = teacherValue.id;
    },
    //获取邮件的状态
    handleSwitchChange(value) {
      if(value){
        //发送邮件
        sentEmail(this.helpMoveform).then(response =>{
          this.$modal.msgSuccess("发送邮件成功");
        });
      }
    },
    submitFormThree() {
      const emailStatus = this.helpMoveform.sendEmail;
      this.handleSwitchChange(emailStatus);

      addHelpMove(this.helpMoveform).then(response => {
        this.$modal.msgSuccess("新增成功");
        this.getList();
        this.moveHelpVisible = false;
      });
    },
    cancelThree() {
      this.moveHelpVisible = false;
    },

    /**
     * 帮扶移交
     */
    handleRemindTransfer(row) {
      this.title = "帮扶移交";
      const now = new Date();
      this.helpMoveform.moveName = row.teacherName;
      this.helpMoveform.helpId = row.id;
      this.helpMoveform.issueType = row.issueType;
      this.helpMoveform.moveNameId = row.teacherId;
      this.helpMoveform.creatTime = now;
      this.moveHelpVisible = true;

    },
    /**
     * 取消帮扶
     */
    cancelTwo() {
      this.dialogVisible = false;
    },
    handleComplete(row) {
      this.finishform.studentName = row.studentName;
      this.finishform.teacherName = row.teacherName;
      this.finishform.email = row.email;
      this.finishform.id = row.id;
      this.dialogVisible = true;
    },
    /**
     * 完成帮扶
     */
    submitFormTwo() {
      //发送请求到后端改变状态和时间
      finishStudentHelp(this.finishform).then(response => {
        this.title = "完成帮扶";
        this.dialogVisible = false;

        this.getList();

      });
      this.form.status
    },

    /** 查询就业帮扶列表 */
    getList() {
      this.loading = true;
      listStudentHelp(this.queryParams).then(response => {
        this.studentHelpList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        studentName: null,
        email: null,
        wechat: null,
        phone: null,
        className: null,
        classId: null,
        creatTime: null,
        issueType: null,
        teacherName: null,
        teacherId: null,
        status: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加就业帮扶";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getStudentHelp(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改就业帮扶";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateStudentHelp(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStudentHelp(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除就业帮扶编号为"' + ids + '"的数据项？').then(function () {
        return delStudentHelp(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('employment/studentHelp/export', {
        ...this.queryParams
      }, `studentHelp_${new Date().getTime()}.xlsx`)
    }
  },
  mounted() {
    //查询所有的老师
    listTeacher(this.queryParams).then(response => {
      this.teacherList = response.rows;
      console.log(response.rows);
      this.total = response.total;
      this.loading = false;
    });
  },
};
</script>
